package ch.nostromo.tiffanys.game.board;

import ch.nostromo.tiffanys.game.game.Game;

public class BoardTools {

  public static boolean isEmptyField(Board board, String coord) throws BoardException {
    int idx = coordToField(coord);
    return board.isEmptyField(idx);
  }

  public static int getPiece(Board board, String coord) throws BoardException {
    int idx = coordToField(coord);
    return board.getPieceCode(idx);
  }

  public static int getPieceColor(Board board, String coord) throws BoardException {
    int idx = coordToField(coord);
    int owner = board.getPieceOwner(idx);
    if (owner == Board.OWNER_BLACK) {
      return Game.COLOR_BLACK;
    } else {
      return Game.COLOR_WHITE;
    }
  }

  public static String fieldToCoord(int field) {
    return getRowDescription(field) + getCollumnDescription(field);
  }

  public static int coordToField(String description) throws BoardException {
    try {
      int result = 0;
      if (description.substring(0, 1).equalsIgnoreCase("A")) {
        result = 21;
      } else if (description.substring(0, 1).equalsIgnoreCase("B")) {
        result = 22;
      } else if (description.substring(0, 1).equalsIgnoreCase("C")) {
        result = 23;
      } else if (description.substring(0, 1).equalsIgnoreCase("D")) {
        result = 24;
      } else if (description.substring(0, 1).equalsIgnoreCase("E")) {
        result = 25;
      } else if (description.substring(0, 1).equalsIgnoreCase("F")) {
        result = 26;
      } else if (description.substring(0, 1).equalsIgnoreCase("G")) {
        result = 27;
      } else if (description.substring(0, 1).equalsIgnoreCase("H")) {
        result = 28;
      } else {
        throw new IllegalArgumentException("Unknown collumn!");
      }

      String rowStr = description.substring(1, 2);
      int row = Integer.valueOf(rowStr).intValue() - 1;

      result += (row * 10);
      return result;

    } catch (Throwable t) {
      throw new BoardException("Unknown row!", t);
    }

  }

  public static String getCollumnDescription(int pos) {
    if (pos > 20 && pos < 29) {
      return "1";
    } else if (pos > 30 && pos < 39) {
      return "2";
    } else if (pos > 40 && pos < 49) {
      return "3";
    } else if (pos > 50 && pos < 59) {
      return "4";
    } else if (pos > 60 && pos < 69) {
      return "5";
    } else if (pos > 70 && pos < 79) {
      return "6";
    } else if (pos > 80 && pos < 89) {
      return "7";
    } else if (pos > 90 && pos < 99) {
      return "8";
    }

    throw new IllegalArgumentException("Unknown collumn!");
  }

  public static String getRowDescription(int pos) {
    int tmp = (pos / 10);
    tmp = tmp * 10;
    if (pos - tmp == 1) {
      return "a";
    } else if (pos - tmp == 2) {
      return "b";
    } else if (pos - tmp == 3) {
      return "c";
    } else if (pos - tmp == 4) {
      return "d";
    } else if (pos - tmp == 5) {
      return "e";
    } else if (pos - tmp == 6) {
      return "f";
    } else if (pos - tmp == 7) {
      return "g";
    } else if (pos - tmp == 8) {
      return "h";
    }

    throw new IllegalArgumentException("Unknown row!");

  }

  public static int getPieceCodeByDescription(String pieceDescription) {
    if (pieceDescription.equalsIgnoreCase("K")) {
      return Board.PIECE_KING;
    } else if (pieceDescription.equalsIgnoreCase("Q")) {
      return Board.PIECE_QUEEN;
    } else if (pieceDescription.equalsIgnoreCase("N")) {
      return Board.PIECE_KNIGHT;
    } else if (pieceDescription.equalsIgnoreCase("B")) {
      return Board.PIECE_BISHOP;
    } else if (pieceDescription.equalsIgnoreCase("R")) {
      return Board.PIECE_ROOK;
    } else if (pieceDescription.equalsIgnoreCase("P")) {
      return Board.PIECE_PAWN;
    }

    throw new IllegalArgumentException("Unknown piece description");
  }

  public static String getPieceDescriptionByPiceCode(int pieceCode) {
    if (pieceCode == Board.PIECE_PAWN) {
      return "p";
    } else if (pieceCode == Board.PIECE_ROOK) {
      return "R";
    } else if (pieceCode == Board.PIECE_BISHOP) {
      return "B";
    } else if (pieceCode == Board.PIECE_KNIGHT) {
      return "N";
    } else if (pieceCode == Board.PIECE_QUEEN) {
      return "Q";
    } else if (pieceCode == Board.PIECE_KING) {
      return "K";
    }
    throw new IllegalArgumentException("Unknown piece code");
  }

}
